package com.example.shop.common.service.impl;

import com.example.shop.common.bo.maplocation.InverseLocationInfo;
import com.example.shop.common.bo.maplocation.Location;
import com.example.shop.common.bo.maplocation.LocationInfo;
import com.example.shop.common.bo.maplocation.LocationResultInfo;
import com.example.shop.common.service.MapLocationService;
import com.example.shop.common.util.HttpURLConnectionHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Service
public class MapLocationServiceImpl implements MapLocationService {
    @Value("${map-location.ak}")
    private String AK;

    private final ObjectMapper objectMapper;

    public MapLocationServiceImpl(@Qualifier("esObjectMapper") ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public LocationInfo getLocationInfo(Location location) throws RuntimeException{
       try{
           URL url = new URL("https://api.map.baidu.com/reverse_geocoding/v3/?" +
                   "ak=" + AK + "&output=json" + "&coordtype=bd09ll" +
                   "&location="+location.getLat()+","+location.getLng());
           HttpURLConnectionHelper connect = new HttpURLConnectionHelper(url);
           connect.connect();
           byte[] responseBody = connect.getResponseBody();
           String json = new String(responseBody, StandardCharsets.UTF_8);
           LocationResultInfo<LocationInfo> result = objectMapper.readValue(json, new TypeReference<LocationResultInfo<LocationInfo>>(){});
           if(result.getStatus()==0){
               LocationInfo locationInfo = result.getResult();
               return locationInfo;
           }
           throw new RuntimeException("common模块的MapLocationServiceImpl的getLocationInfo定位异常");
       }catch (Exception e){
           e.printStackTrace();
       }
        return null;
    }


    @Override
    public InverseLocationInfo getLocationInfo(String area) throws RuntimeException {
        try{
            URL url = new URL("https://api.map.baidu.com/geocoding/v3/?" +
                    "address=" + area + "&ak=" + AK + "&output=json&coordtype=bd09ll");
            HttpURLConnectionHelper connect = new HttpURLConnectionHelper(url);
            connect.connect();
            byte[] responseBody = connect.getResponseBody();
            String json = new String(responseBody, StandardCharsets.UTF_8);
            LocationResultInfo<InverseLocationInfo> result = objectMapper.readValue(json, new TypeReference<LocationResultInfo<InverseLocationInfo>>(){});
            if(result.getStatus()==0){
                InverseLocationInfo inverseLocationInfo = result.getResult();
                return inverseLocationInfo;
            }
            throw new RuntimeException("common模块的MapLocationServiceImpl的getLocationInfo定位异常");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
